package cn.jinjimi.domain.data.imp;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.jinjimi.app.entity.LinkBean;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.domain.data.Store;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.AdvService;
import cn.jinjimi.domain.store.AdvStore;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * Created by Roy
 * Date: 16/8/19
 */
public class AdvStoreImp extends Store implements AdvStore {

    @Override
    public List<LinkBean> getAdvList(String position) throws IOException {
        Response<ResponseBody> response = API.create(AdvService.class).getAdvs(position).execute();
        ArrayList<LinkBean> result = new ArrayList<>();
        if (response.isSuccessful()) {
            Type type = new TypeToken<List<LinkBean>>(){}.getType();
            List<LinkBean> list = GsonUtils.fromJson(response.body().string(), type);
            if (list != null) {
                result.addAll(list);
            }
        }
        return result;
    }
}
